package cn.zwx.distributed.id.server.service;

import cn.zwx.common.Result;
import cn.zwx.common.Status;
import cn.zwx.distributed.id.server.exception.LeafServerException;
import cn.zwx.service.SegmentService;
import cn.zwx.service.SnowflakeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangwenxue
 */
@Service
public class DistributedServiceImpl implements DistributedService{

    @Resource
    private SegmentService segmentService;
    @Resource
    private SnowflakeService snowflakeService;

    @Override
    public String getSegmentId(String key) {
        Result result = segmentService.getId(key);
        if (result.getStatus().equals(Status.EXCEPTION)) {
            throw new LeafServerException(result.toString());
        }
        return String.valueOf(result.getId());
    }

    @Override
    public String getSnowflakeId(String key) {
        Result result = snowflakeService.getId(key);
        if (result.getStatus().equals(Status.EXCEPTION)) {
            throw new LeafServerException(result.toString());
        }
        return String.valueOf(result.getId());
    }
}
